package com.example.taximeter.service.impl;

import com.example.taximeter.bean.Company;
import com.example.taximeter.bean.ReturnInfo;
import com.example.taximeter.bean.UserView;
import com.example.taximeter.mapper.CompanyMapper;
import com.example.taximeter.mapper.TaximeterMapper;
import com.example.taximeter.service.CompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 出租车公司
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    private CompanyMapper companyMapper;
    private TaximeterMapper taximeterMapper;

    @Autowired
    public CompanyServiceImpl(CompanyMapper companyMapper,TaximeterMapper taximeterMapper){
        this.companyMapper = companyMapper;
        this.taximeterMapper = taximeterMapper;
    };

    @Override
    public List<Company> findCompanyList() {

        return companyMapper.findCompanyList();
    }

    @Override
    public ReturnInfo findCompanyALL(Integer pageNum, Integer pageSize, Company company) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<Company> companies = companyMapper.findCompanyAll(company);
        PageInfo<Company> pageInfo = new PageInfo<>(companies);
        if(companies.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }

    @Override
    public String addCompany(Company company) {
        if(companyMapper.findCompanyByCompanyname(company.getCompanyname()).size()!=0){
            return "出租车公司名重复";
        }else if(companyMapper.addCompany(company)){
            return "成功";
        }else {
            return "失败";
        }

    }

    @Override
    public String updateCompany(Company company) {
        List<Company> companies = companyMapper.findCompanyByCompanyname(company.getCompanyname());
        if(companies.size()==1){
            if(companies.get(0).getId()!=company.getId()){
                return "公司名重复";
            }else {
                if(companyMapper.updateCompany(company)){
                    return "成功";
                }else {
                    return "失败";
                }
            }
        }else if(companies.size()>1){
            return "公司名重复";
        }else {
            if(companyMapper.updateCompany(company)){
                return "成功";
            }else {
                return "失败";
            }
        }
    }



    @Override
    public String deleteCompany(Integer id) {
        if (taximeterMapper.findTaximeterByCompanyid(id).size()>0){
            return "该出租车公司已被引用";
        }else {
            if(companyMapper.deleteCompany(id)){
                return "成功";
            }else {
                return "失败";
            }
        }

    }
}
